package com.jic.point.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.RestResult;
import com.jic.point.constant.consist.ResponseCode;
import com.jic.point.model.entity.SettSupplierBalance;
import com.jic.point.model.vo.request.SettSupplierBalanceRequest;
import com.jic.point.model.vo.response.SettSupplierBalanceResponse;
import com.jic.point.service.SettSupplierBalanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: tzc
 * @date: 2020-10-11 17:28:33
 */
@RestController
@Slf4j
@RequestMapping("/settSupplierBalance")
@Api(tags = " [供应商结算台账表]控制类-PC")
public class SettSupplierBalanceController {
    @Autowired
    private SettSupplierBalanceService service;



    /**
     * 新增记录
     *
     * @param request 新增[供应商结算台账表]请求参数
     * @return SettSupplierBalanceResponse 新增[供应商结算台账表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增[供应商结算台账表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody SettSupplierBalanceRequest request) throws Throwable {
        SettSupplierBalance settSupplierBalance = new SettSupplierBalance();
        BeanUtils.copyProperties(request, settSupplierBalance);
        long flag = service.save(settSupplierBalance);
        return RestResult.success(flag);
    }

    /**
     * 新增或修改记录
     *
     * @param request 新增或修改[供应商结算台账表]请求参数
     * @return SettSupplierBalanceResponse 新增或修改[供应商结算台账表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "保存或更新[供应商结算台账表]")
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public RestResult<Long> saveOrUpdate(@Validated @RequestBody SettSupplierBalanceRequest request) throws Throwable {
        SettSupplierBalance settSupplierBalance = new SettSupplierBalance();
        BeanUtils.copyProperties(request, settSupplierBalance);
        long flag = service.saveOrUpdate(settSupplierBalance);
        return RestResult.success(flag);
    }

    /**
     * 删除记录
     *
     * @param request 删除[供应商结算台账表]请求参数
     * @return SettSupplierBalanceResponse 删除[供应商结算台账表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "删除[供应商结算台账表]记录")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult<Long> delete(@RequestBody SettSupplierBalanceRequest request) throws Throwable {
        SettSupplierBalance settSupplierBalance = new SettSupplierBalance();
        BeanUtils.copyProperties(request, settSupplierBalance);
        long flag = service.deleteByPrimaryKeyLogically(settSupplierBalance);
        return RestResult.success(flag);
    }

    /**
     * 修改记录
     *
     * @param request 修改[供应商结算台账表]请求参数
     * @return SettSupplierBalanceResponse 修改[供应商结算台账表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "修改[供应商结算台账表]记录")
    @PostMapping("/update")
    @ResponseBody
    public RestResult<Long> update(@RequestBody SettSupplierBalanceRequest request) throws Throwable {
        SettSupplierBalance settSupplierBalance = new SettSupplierBalance();
        BeanUtils.copyProperties(request, settSupplierBalance);
        long flag = service.updateByPrimaryKeySelective(settSupplierBalance);
        return RestResult.success(flag);
    }

    /**
     * 单表查询详情信息
     *
     * @param request 查询[供应商结算台账表]详情请求参数
     * @return SettSupplierBalanceResponse 查询[供应商结算台账表]详情响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @PostMapping("/getByPrimaryKey")
    @ApiOperation(value = "查询[供应商结算台账表]详情")
    @ResponseBody
    public RestResult<SettSupplierBalanceResponse> getByPrimaryKey(@Validated @RequestBody SettSupplierBalanceRequest request) {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        if (request != null && request.getId() != null) {

            SettSupplierBalance settSupplierBalance = new SettSupplierBalance();
            BeanUtils.copyProperties(request, settSupplierBalance);
            SettSupplierBalance result = service.getByPrimaryKey(settSupplierBalance);
            log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
            if (result == null) {
                return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
            }
            SettSupplierBalanceResponse bo = new SettSupplierBalanceResponse();
            BeanUtils.copyProperties(result, bo);
            return RestResult.success(bo);
        }
        log.info("查询详情请求结束");
        return RestResult.success(new SettSupplierBalanceResponse());
    }

    /**
     * 分页查询
     *
     * @param request 分页查询请求参数
     * @return 分页列表
     * @throws Throwable 业务异常和系统异常
     */
    @PostMapping("/listPage")
    @ApiOperation(value = " 分页查询列表 ")
    @ResponseBody
    public RestResult<PageInfo<SettSupplierBalance>> listPage(@RequestBody SettSupplierBalanceRequest request) throws Throwable {
        SettSupplierBalance param = new SettSupplierBalance();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        return RestResult.success(service.listPageBySelective(param, request.getPageNum(), request.getPageSize()));
    }


    /**
     * SETT_SUPPLIER_BALANCE 查询列表（不分页）
     *
     * @param request 前端请求参数
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "不分页查询 [供应商结算台账表] ")
    @ResponseBody
    public RestResult<List<SettSupplierBalanceResponse>> listSettSupplierBalanceAllRecord(@RequestBody SettSupplierBalanceRequest request) throws Throwable {
        SettSupplierBalance param = new SettSupplierBalance();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        List<SettSupplierBalance> list = service.listAllRecord(param);
        List<SettSupplierBalanceResponse> responseList = list.stream().map(temp -> {
            SettSupplierBalanceResponse bo = new SettSupplierBalanceResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }

    @PostMapping("/account")
    @ApiOperation(value = "结算[供应商结算台账表] ")
    @ResponseBody
    public RestResult<Long> account(@RequestBody SettSupplierBalanceRequest request){
        SettSupplierBalance param=new SettSupplierBalance();
        BeanUtils.copyProperties(request,param);
       long flag= service.updateById(param);
        return RestResult.success(flag);
    }

    @PostMapping("/addBalance")
    @ApiOperation(value = "[供应商结算台账容错] ")
    @ResponseBody
    public RestResult<Long> addBalance(@RequestBody SettSupplierBalanceRequest request){
        SettSupplierBalance param=new SettSupplierBalance();
        BeanUtils.copyProperties(request,param);
        long flag= service.updateByPrimaryKey(param,request.getType(),request.getIds());
        return RestResult.success(flag);
    }
}

	
